package com.firstgroup.dms.service.impl;

import com.firstgroup.dms.common.Result;
import com.firstgroup.dms.mapper.EmployeeMapper;
import com.firstgroup.dms.mapper.UserMapper;
import com.firstgroup.dms.pojo.Employee;
import com.firstgroup.dms.pojo.User;
import com.firstgroup.dms.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Employee> getAllEmployees() {
        return employeeMapper.selectAll();
    }

    @Override
    public Result<Employee> getById(Integer id) {
        try {
            Employee employee = employeeMapper.getById(id);
            if (employee != null) {
                return Result.success(employee);
            }
            return Result.error("员工不存在");
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    @Override
    public Result<Object> add(Employee employee) {
        // 先创建用户账号
        User user = new User();
        user.setUserName(employee.getName());
        user.setUserPwd("123456"); // 默认密码
        userMapper.insert(user);

        // 再添加员工信息
        employeeMapper.insert(employee);
        return Result.success();
    }

    @Override
    public Result<Object> update(Employee employee) {
        try {
            int rows = employeeMapper.update(employee);
            if (rows > 0) {
                return Result.success();
            }
            return Result.error("更新失败");
        } catch (Exception e) {
            return Result.error("更新失败：" + e.getMessage());
        }
    }

    @Override
    public Result<Object> deleteById(Integer id) {
        try {
            int rows = employeeMapper.deleteById(id);
            if (rows > 0) {
                return Result.success();
            }
            return Result.error("删除失败");
        } catch (Exception e) {
            return Result.error("删除失败：" + e.getMessage());
        }
    }

    @Override
    public Result<Map<String, Object>> getList(Employee condition, Integer page, Integer limit) {
        try {
            // 设置分页参数
            condition.setStartIndex((page - 1) * limit);
            condition.setPageSize(limit);

            // 查询数据
            List<Employee> list = employeeMapper.getByCondition(condition);
            Long count = employeeMapper.getCount(condition);

            // 组装返回结果
            Map<String, Object> result = new HashMap<>();
            result.put("code", 0);
            result.put("msg", "");
            result.put("count", count);
            result.put("data", list);

            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }
}